<?php


namespace app\common\cachemodel\active;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Active1SignIn extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.active1_sign_in';

    protected $cacheModel = 'active.active_sign_in';

    /**
     * 签到
     * @param int $activeFlag
     * @param int $uid
     * @param int $date
     * @return bool
     */
    public function signIn(int $activeFlag, int $uid, int $date)
    {
        try
        {
            $this->save([
                'active_flag' => $activeFlag,
                'uid' => $uid,
                'date' => $date,
                'create_time' => time(),
            ]);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $uid)->setCache([$date => $date]);

            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Active1SignIn model => signIn()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

            return false;
        }
    }

    /**
     * 获取已签到 数据
     * @param int $activeFlag 活动标识
     * @param int $uid 用户uid
     * @return array
     * @throws \Exception
     */
    public function getSignInData(int $activeFlag, int $uid): array
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $uid)->getDataAndSet(function () use ($activeFlag, $uid)
        {
            try
            {
                $data = $this->where(['active_flag' => $activeFlag, 'uid' => $uid])->column('date');

                $da = [0 => 0];
                foreach ($data as $item)
                {
                    $da[$item] = $item;
                }

                return $da;
            }
            catch (\Exception $e)
            {
                Log::write('Active1SignIn model => getSignInData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

                return [];
            }
        });

        unset($data[0]);

        return $data;
    }
}
